package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.RukuDao;
import com.aaa.qy127.entity.Ruku;
import com.aaa.qy127.util.JDBCUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class RukuDaoImpl implements RukuDao {
    /**
     * 查询所有入库信息
     * @param ruku
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAllruku(Ruku ruku) {
        StringBuilder sql = new StringBuilder("select rukuid,rdate,staffName,totalprice,totalcount,jsr\n" +
                "from t_ruku r \n" +
                "join t_staff s\n" +
                "on s.id = r.uid where 1=1 and r.status = 1");
        if (ruku.getStaffName()!=null && !"".equals(ruku.getStaffName())){
            sql.append(" and staffName like '%").append(ruku.getStaffName()).append("%'");
        }
        if (ruku.getJsr()!=null && !"".equals(ruku.getJsr())){
            sql.append(" and jsr like '%").append(ruku.getJsr()).append("%'");
        }
        if (ruku.getOnTime()!=null && !"".equals(ruku.getOnTime())){
            sql.append(" and r.rdate >= '").append(ruku.getOnTime()).append("'");
        }
        if (ruku.getOffTime()!=null && !"".equals(ruku.getOffTime())){
            sql.append(" and r.rdate <= '").append(ruku.getOffTime()).append("'");
        }
        sql.append(" limit ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql.toString(),(ruku.getPage()-1)*ruku.getLimit(),ruku.getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 查询入库信息的条数
     * @param ruku
     * @return
     */
    @Override
    public int getAllCount(Ruku ruku) {
        StringBuilder sql = new StringBuilder("select count(rukuid)\n" +
                "from t_ruku r \n" +
                "join t_staff s\n" +
                "on s.id = r.uid where 1=1 and r.status = 1");
        if (ruku.getStaffName()!=null && !"".equals(ruku.getStaffName())){
            sql.append(" and staffName like '%").append(ruku.getStaffName()).append("%'");
        }
        if (ruku.getJsr()!=null && !"".equals(ruku.getJsr())){
            sql.append(" and jsr like '%").append(ruku.getJsr()).append("%'");
        }
        if (ruku.getOnTime()!=null && !"".equals(ruku.getOnTime())){
            sql.append(" and r.rdate >= '").append(ruku.getOnTime()).append("'");
        }
        if (ruku.getOffTime()!=null && !"".equals(ruku.getOffTime())){
            sql.append(" and r.rdate <= '").append(ruku.getOffTime()).append("'");
        }
        List<List> lists = JDBCUtil.queryList(sql.toString());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
    /**
     * 查询一条入库信息的详情信息
     * @param ruku
     * @return
     */
    @Override
    public List<Map<String, Object>> getoneruku(Ruku ruku) {
        String sql = "SELECT r.rdid,r.rukuid,b.bookname,b.author,b.publisher,r.count,r.pushprice from t_rukudetail r\n" +
                "JOIN t_book b\n" +
                "on r.bookid = b.bookid\n" +
                "where r.rukuid = ? limit ?,?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql,ruku.getRukuid(),(ruku.getPage()-1)*ruku.getLimit(),ruku.getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 查询一条入库信息详情的条数
     * @param ruku
     * @return
     */
    @Override
    public int getoneCount(Ruku ruku) {
        String sql = "select count(*) from t_rukudetail where rukuid = ?";
        List<List> lists = JDBCUtil.queryList(sql,ruku.getRukuid());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
    /**
     * 添加入库信息
     * @param uid
     * @param totalprice
     * @param totalcount
     * @param jsr
     * @return
     */
    @Override
    public int addruku(Integer uid, double totalprice, Integer totalcount, String jsr) {
        String sql = "insert into t_ruku values(null,?,?,?,?,?,0)";
        int primaryKey = JDBCUtil.getPrimaryKey(sql, new Date(), uid, totalprice, totalcount, jsr);
        return primaryKey;
    }
    /**
     * 添加入库详情信息
     * @param bookid
     * @param count
     * @param rukuid
     * @param pushprice
     * @return
     */
    @Override
    public int addrukuxiangqing(Integer bookid, Integer count, Integer rukuid, double pushprice) {
        String sql = "insert into t_rukudetail values(null,?,?,?,?)";
        int update = JDBCUtil.update(sql, bookid, count, rukuid, pushprice);
        return update;
    }
}
